﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class TaskManagerUserDA : ITaskManagerUserDA
    {
        private string CREATED_BY = "@CREATED_BY";
        private string EQUIPMENT_PROFILE_ID = "@EQUIPMENT_PROFILE_ID";
        private string LAST_CASE_ID_PICKED = "@LAST_CASE_ID_PICKED";
        private string LAST_LOC = "@LAST_LOC";
        private string LAST_TO_LOC = "@LAST_TO_LOC";
        private string LAST_WAVE_ID = "@LAST_WAVE_ID";
        private string PRIORITY_TASK_TYPE = "@PRIORITY_TASK_TYPE";
        private string SQL_CHECK_TASK_MANAGER_USER_ID_UNIQUE = " SELECT COUNT(1) FROM @_@TASK_MANAGER_USER WHERE USER_ID = @USER_ID ";
        private string SQL_DELETE_TASK_MANAGER_USER = " DELETE FROM @_@TASK_MANAGER_USER WHERE USER_ID = @USER_ID ";
        private string SQL_INSERT_TASK_MANAGER_USER = " INSERT INTO @_@TASK_MANAGER_USER ( WH_ID, USER_ID, TASK_STRATEGY_ID, USER_GROUP, EQUIPMENT_PROFILE_ID, PRIORITY_TASK_TYPE, LAST_CASE_ID_PICKED, LAST_WAVE_ID, LAST_LOC, LAST_TO_LOC, USER_NAME, USER_STATUS, USER_FNAME, USER_LNAME, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @USER_ID, @TASK_STRATEGY_ID, @USER_GROUP, @EQUIPMENT_PROFILE_ID, @PRIORITY_TASK_TYPE, @LAST_CASE_ID_PICKED, @LAST_WAVE_ID, @LAST_LOC, @LAST_TO_LOC, @USER_NAME, @USER_STATUS, @USER_FNAME, @USER_LNAME, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_TASK_MANAGER_USER = " SELECT  ROWID, WH_ID, USER_ID, TASK_STRATEGY_ID, USER_GROUP, EQUIPMENT_PROFILE_ID, PRIORITY_TASK_TYPE,  LAST_CASE_ID_PICKED, LAST_WAVE_ID, LAST_LOC, LAST_TO_LOC, USER_NAME, USER_STATUS, USER_FNAME, USER_LNAME, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM  @_@TASK_MANAGER_USER ";
        private string SQL_UPDATE_TASK_MANAGER_USER = " UPDATE @_@TASK_MANAGER_USER SET WH_ID = @WH_ID, TASK_STRATEGY_ID = @TASK_STRATEGY_ID, USER_GROUP = @USER_GROUP, EQUIPMENT_PROFILE_ID = @EQUIPMENT_PROFILE_ID, PRIORITY_TASK_TYPE = @PRIORITY_TASK_TYPE, LAST_CASE_ID_PICKED = @LAST_CASE_ID_PICKED, LAST_WAVE_ID = @LAST_WAVE_ID, LAST_LOC = @LAST_LOC, LAST_TO_LOC = @LAST_TO_LOC, USER_NAME = @USER_NAME, USER_STATUS = @USER_STATUS, USER_FNAME = @USER_FNAME, USER_LNAME = @USER_LNAME, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE USER_ID = @USER_ID  ";
        private string TASK_STRATEGY_ID = "@TASK_STRATEGY_ID";
        private string UPDATED_BY = "@UPDATED_BY";
        private string USER_GROUP = "@USER_GROUP";
        private string USER_ID = "@USER_ID";
        private string USR_FNAME = "@USER_FNAME";
        private string USR_LNAME = "@USER_LNAME";
        private string USR_NAME = "@USER_NAME";
        private string USR_STATUS = "@USER_STATUS";
        private string WH_ID = "@WH_ID";

        public bool CheckTaskManagerUserIDUnique(string taskManagerUserID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@USER_ID", taskManagerUserID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_TASK_MANAGER_USER_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckTaskManagerUserIDUnique(DataBase dataBase, DbTransaction tran, string taskManagerUserID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@USER_ID", taskManagerUserID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_TASK_MANAGER_USER_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteTaskManagerUser(List<string> taskManagerUserID, string whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int j = 0; j < taskManagerUserID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@USER_ID", taskManagerUserID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_TASK_MANAGER_USER.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteTaskManagerUser(DataBase dataBase, DbTransaction tran, List<string> taskManagerUserID, string whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; taskManagerUserID.Count > i; i++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@USER_ID", taskManagerUserID[i]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_TASK_MANAGER_USER.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllTaskManagerUser(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_TASK_MANAGER_USER.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllTaskManagerUser(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_TASK_MANAGER_USER.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public TaskManagerUserInfo GetTaskManagerUserByID(string taskmanageruserID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_TASK_MANAGER_USER.Replace("@_@", whLoginID) + " WHERE USER_ID = @USER_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@USER_ID", taskmanageruserID) };
            TaskManagerUserInfo taskManagerUserInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    taskManagerUserInfo = new TaskManagerUserInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["USER_ID"].ToString(), reader["TASK_STRATEGY_ID"].ToString(), reader["USER_GROUP"].ToString(), reader["EQUIPMENT_PROFILE_ID"].ToString(), reader["PRIORITY_TASK_TYPE"].ToString(), reader["LAST_CASE_ID_PICKED"].ToString(), reader["LAST_WAVE_ID"].ToString(), reader["LAST_LOC"].ToString(), reader["LAST_TO_LOC"].ToString(), reader["USER_NAME"].ToString(), (reader["USER_STATUS"].ToString() != "") ? int.Parse(reader["USER_STATUS"].ToString()) : 0, reader["USER_FNAME"].ToString(), reader["USER_LNAME"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return taskManagerUserInfo;
        }

        public TaskManagerUserInfo GetTaskManagerUserByID(DataBase dataBase, DbTransaction tran, string taskmanageruserID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_TASK_MANAGER_USER.Replace("@_@", whLoginID) + " WHERE USER_ID = @USER_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@USER_ID", taskmanageruserID) };
            TaskManagerUserInfo taskManagerUserInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                taskManagerUserInfo = new TaskManagerUserInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["USER_ID"].ToString(), reader["TASK_STRATEGY_ID"].ToString(), reader["USER_GROUP"].ToString(), reader["EQUIPMENT_PROFILE_ID"].ToString(), reader["PRIORITY_TASK_TYPE"].ToString(), reader["LAST_CASE_ID_PICKED"].ToString(), reader["LAST_WAVE_ID"].ToString(), reader["LAST_LOC"].ToString(), reader["LAST_TO_LOC"].ToString(), reader["USER_NAME"].ToString(), (reader["USER_STATUS"].ToString() != "") ? int.Parse(reader["USER_STATUS"].ToString()) : 0, reader["USER_FNAME"].ToString(), reader["USER_LNAME"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return taskManagerUserInfo;
        }

        public DataSet GetTaskManagerUserByQueryList(List<string> sqlWhere, TaskManagerUserQueryEntity taskmanageruserQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_TASK_MANAGER_USER.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!taskmanageruserQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, taskmanageruserQuery.CurrentPage, taskmanageruserQuery.PageSize, taskmanageruserQuery.SortField, taskmanageruserQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetTaskManagerUserByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, TaskManagerUserQueryEntity taskmanageruserQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_TASK_MANAGER_USER.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            temp = PagingHelper.GetPagingSQL(temp, taskmanageruserQuery.CurrentPage, taskmanageruserQuery.PageSize, taskmanageruserQuery.SortField, taskmanageruserQuery.SortDirection);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public int InsertTaskManagerUser(TaskManagerUserInfo taskManagerUserInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                taskManagerUserInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_TaskManagerUser_Parameters(taskManagerUserInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_TASK_MANAGER_USER.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertTaskManagerUser(DataBase dataBase, DbTransaction tran, TaskManagerUserInfo taskManagerUserInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                taskManagerUserInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_TaskManagerUser_Parameters(taskManagerUserInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_TASK_MANAGER_USER.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_TaskManagerUser_Parameters(TaskManagerUserInfo taskManagerUserInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.USER_ID, SqlDbType.VarChar, 20), new SqlParameter(this.TASK_STRATEGY_ID, SqlDbType.VarChar, 10), new SqlParameter(this.USER_GROUP, SqlDbType.VarChar, 10), new SqlParameter(this.EQUIPMENT_PROFILE_ID, SqlDbType.VarChar, 10), new SqlParameter(this.PRIORITY_TASK_TYPE, SqlDbType.VarChar, 10), new SqlParameter(this.LAST_CASE_ID_PICKED, SqlDbType.VarChar, 20), new SqlParameter(this.LAST_WAVE_ID, SqlDbType.VarChar, 10), new SqlParameter(this.LAST_LOC, SqlDbType.VarChar, 10), new SqlParameter(this.LAST_TO_LOC, SqlDbType.VarChar, 30), new SqlParameter(this.USR_NAME, SqlDbType.VarChar, 40), new SqlParameter(this.USR_STATUS, SqlDbType.Int, 4), new SqlParameter(this.USR_FNAME, SqlDbType.VarChar, 0x19), new SqlParameter(this.USR_LNAME, SqlDbType.VarChar, 0x19), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20) };
            if (!string.IsNullOrEmpty(taskManagerUserInfo.WhID))
            {
                paramArray[0].Value = taskManagerUserInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = taskManagerUserInfo.UserID;
            if (!string.IsNullOrEmpty(taskManagerUserInfo.TaskStrategyID))
            {
                paramArray[2].Value = taskManagerUserInfo.TaskStrategyID;
            }
            else
            {
                paramArray[2].Value = DBNull.Value;
            }
            paramArray[3].Value = taskManagerUserInfo.UserGroup;
            paramArray[4].Value = taskManagerUserInfo.EquipmentProfileID;
            paramArray[5].Value = taskManagerUserInfo.PriorityTaskType;
            paramArray[6].Value = taskManagerUserInfo.LastCaseIdPicked;
            paramArray[7].Value = taskManagerUserInfo.LastWaveID;
            paramArray[8].Value = taskManagerUserInfo.LastLoc;
            if (!string.IsNullOrEmpty(taskManagerUserInfo.LastToLoc))
            {
                paramArray[9].Value = taskManagerUserInfo.LastToLoc;
            }
            else
            {
                paramArray[9].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(taskManagerUserInfo.UsrName))
            {
                paramArray[10].Value = taskManagerUserInfo.UsrName;
            }
            else
            {
                paramArray[10].Value = DBNull.Value;
            }
            paramArray[11].Value = taskManagerUserInfo.UsrStatus;
            if (!string.IsNullOrEmpty(taskManagerUserInfo.UsrFname))
            {
                paramArray[12].Value = taskManagerUserInfo.UsrFname;
            }
            else
            {
                paramArray[12].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(taskManagerUserInfo.UsrLname))
            {
                paramArray[13].Value = taskManagerUserInfo.UsrLname;
            }
            else
            {
                paramArray[13].Value = DBNull.Value;
            }
            paramArray[14].Value = taskManagerUserInfo.CreatedBy;
            paramArray[15].Value = taskManagerUserInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateTaskManagerUser(TaskManagerUserInfo taskManagerUserInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                taskManagerUserInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_TaskManagerUser_Parameters(taskManagerUserInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_TASK_MANAGER_USER.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateTaskManagerUser(DataBase dataBase, DbTransaction tran, TaskManagerUserInfo taskManagerUserInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                taskManagerUserInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_TaskManagerUser_Parameters(taskManagerUserInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_TASK_MANAGER_USER.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

